<template>
  <Form class="xtx-form" ref="target" :validation-schema="rules" v-slot="{errors}">
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，欢迎{{nickname}}，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field
         v-model="formData.mobile"
         name="mobile"
         class="input"
         type="text"
         :class="{error:errors.mobile}"
         placeholder="绑定的手机号" />
      </div>
      <div class="error" v-if="errors.mobile"><i class="iconfont icon-warning" />{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
     <div class="field">
       <i class="icon iconfont icon-code"></i>
       <Field
        v-model="formData.code"
        name="code"
        class="input"
        type="text"
        :class="{error:errors.code}"
        placeholder="短信验证码" />
        <span class="code" @click="send">
          {{time===0 ? '发送验证码': `${time}秒后发送`}}
        </span>
     </div>
       <div class="error" v-if="errors.code"><i class="iconfont icon-warning" />{{errors.code}}</div>
    </div>
    <a @click="submit" href="javascript:;" class="submit">立即绑定</a>
  </Form>
</template>

<script>
import { useStore } from 'vuex'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { Form, Field } from 'vee-validate'
import { userQQBindCode } from '../../../api/user'
import { useCountdown } from '@/compositions'
import { mobile, code } from '@/utils/validata'
import QC from 'qc'
import Message from '@/components/Message'
export default {
  name: 'CallbackBind',
  components: { Form, Field },
  props: { unionId: { type: String, default: '' } },
  setup (props) {
    const nickname = ref('')
    const avatar = ref('')
    if (QC.Login.check()) {
      QC.api('get_user_info').success(res => {
        console.log('get_user_info', res)
        avatar.value = res.data.figureurl_2
        nickname.value = res.data.nickname
      })
    }
    // 定义数据项
    const formData = reactive({
      mobile: '13241051259',
      code: ''
    })
    // 定义校验规则
    const rules = {
      mobile: mobile,
      code: code
    }
    const router = useRouter()
    const store = useStore()
    const target = ref(null)
    const doBind = async () => {
    //   alert('校验通过')
      try {
        await store.dispatch('user/userQQBindLogin', { unionId: props.unionId, mobile: formData.mobile, code: formData.code })
        Message({ type: 'success', text: '绑定成功！' })
        router.push('/')
      } catch (err) {
        console.log(err)
        Message({ type: 'error', text: '绑定失败!!！' })
      }
    }
    // 兜底校验
    const submit = () => {
      target.value.validate().then(valid => {
        if (valid) {
          doBind()
        }
      })
    }
    const { time, start } = useCountdown()
    // 发验证码
    const send = () => {
      if (mobile(formData.mobile) !== true) {
        return
      }
      if (time.value !== 0) {
        return
      }
      userQQBindCode(formData.mobile).then(data => {
        console.log('userQQBindCode', data)
        Message({ type: 'success', text: '验证码已经发送！' })
        start(60)
      }).catch(err => {
        console.log(err)
        Message({ type: 'error', text: err.response.data.message })
      })
    }
    return { nickname, avatar, formData, rules, target, submit, send, start, time }
  }
}
</script>

<style scoped lang='less'>
.user-info {
    width: 320px;
    height: 70px;
    margin: 0 auto;
    display: flex;
    background: #f2f2f2;
    align-items: center;
    padding: 0 10px;
    margin-bottom: 25px;
    img {
      background: #f2f2f2;
      width: 50px;
      height: 50px;
    }
    p {
      padding-left: 10px;
    }
  }
  .code {
    position: absolute;
    right: 0;
    top: 0;
    line-height: 50px;
    width: 80px;
    color: #999;
    &:hover {
      cursor: pointer;
    }
  }
</style>
